<?php
date_default_timezone_set("Asia/Shanghai");
class Renren {
	const NAME="renren";
	public $target=FALSE;
	public $api_key = "db1ffe772f504e9790907be9c9a651c7";
	public $secret = "a13bb175bc1940f6b78807e58329264f";
	public $auth_token = "";
	public $session_key = "";
	public $url='http://api.renren.com/restserver.do';
	public $phoneUrl = 'http://m3.api.renren.com/restserver.do';
	public $uid;
	public $ticket;
	public $loginUrl='http://login.api.renren.com/CL.do';
	public $data=Array 
					(		
						"method"=>'',
		 				"format" => "json",
		 				"v" => "1.2",
		 				"api_key" => "db1ffe772f504e9790907be9c9a651c7",
		 				"session_key" =>"",
						
					);
	public function __construct($session_key='',$uid='',$ticket=''){
		$this->session_key=$session_key;
		$this->uid=$uid;
		$this->ticket=$ticket;
	
	}
	
	
	/**
	 * ͨ�õ�����api
	 * 
	 * @param array $data ������Ҫpost��request
	 * @param String $url  Ŀ�ĵ�ַ
	 * @return ����array
	 */
	public function api($data = array(),$url='http://login.api.renren.com/CL.do') {
		ksort ( $data );
		$_data = http_build_query ( $data );
		$_data = preg_replace ( "/&/is", '', $_data );
		$_data .= $this->secret;
		$sig = md5 ( $_data );
		
		$data ['sig'] = $sig;
		$o = "";
		foreach ( $data as $k => $v ) {
			$o .= "$k=" . urlencode ( $v ) . "&";
		}
		$data = substr ( $o, 0, - 1 );
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
		$result = curl_exec ( $ch );
		curl_close($ch);
		return $result;
	
	}


	/**
	 * 
	 * �û���¼
	 * @param String $user �û���
	 * @param String $password ����
	 */
	public function login($user,$password) {
//		$data = Array ("check" => "0", "user" => $user, "clientName" => "renren", "model" => "gen", "version" => "20100317", "login_info" => '{"ver":"2.2.0.20100317","cellid":"0","from":9100201}', "format" => "json", "password" =>md5($password), "v" => "1.0", "api_key" => "db1ffe772f504e9790907be9c9a651c7", "session_key" => "" );
		$data["user"]=$user;
		
		$data ['check'] = '0';
		$data ['version'] = '20100317';
		$data ['clientName'] = 'renren';
		$data ['login_info'] = '{"ver":"2.2.0.20100317","cellid":"0","from":9100201}';
		$data ['v'] = '1.0';
		$data ['session_key'] = '';
		$data ['api_key'] = db1ffe772f504e9790907be9c9a651c7;
		$data ['format'] = 'json';
		$data ['password'] = md5 ( $password );
		$result = json_decode ( $this->api ( $data, $this->loginUrl ), true );
		$this->session_key=$result['session_key'];
		$this->ticket=$result['ticket'];
		$this->uid=$result['uid'];
//		setcookie("session_key",$this->session_key);
		
	
	}
	private function feedsCmpByTime($a,$b){
		
	if (strtotime($a['time'])==strtotime($b['time']))
			return 0;
		return strtotime($a['time'])<strtotime($b['time'] ) ? 1 : - 1;
			
	}
	/**
	 * 
	 * ��ȡ�û������£��������顣
	 * @param String $type ��Ϣ���ͣ�10-״̬��20-�û���־��21-������־��30-�ϴ���Ƭ��31-������Ƭ��32-������Ƭ����
	 * @param String $limit ���ص������Ŀ
	 * @param String $offset ƫ����
	 */
	public function getFeedList($limit='30',$offset='0',$type='10'){
		$data = Array 
					(	"offset" => $offset, 	
						"method"=>'phoneclient.getFeedList',
		 				"format" => "json",
		 				"v" => "1.2",
		 				"api_key" => "db1ffe772f504e9790907be9c9a651c7",
		 				"session_key" => $this->session_key,
						"limit"=>$limit, 
						"type"=>$type,
					);
		$result = $this->api ( $data, $this->phoneUrl );
		$result= json_decode ( $result, true );
		$result=$result['newsfeed'];
		
		return $this->resultSort($result);
		
	}
	

	/**
	 * 
	 * ��ȡ�û�����������Ϣ
	 * @param $count
	 * @param $page
	 */
	public function getFriendRequestInfo($count='20',$page='1'){
		$data=$this->data;
		$data['v']='1.1';
		$data['method']="phoneclient.getFriendRequestInfo";
		$data['count']=$count;
		$data['page']=$page;
		$data['session_key']=$this->session_key;
//		return $data;
		return json_decode($this->api($data,$this->phoneUrl),true);
		
	}
	
	/**
	 * 
	 * ��ȡ�û�������Ϣ
	 * @param $uid �û�id
	 * @param $fields ��ȡ������ɸѡ�����ò���basic,feed,gossip,visitor,fangle,birthday��
	 */
	public function _getProfile($uid="",$fields="basic"){
		$data=$this->data;
		$data['v']='1.2';
		if ($uid == "")
			$data ['uid'] = $this->uid;
		$data['method']='phoneclient.getProfile';
		$data['fields']=$fields;
		$data['session_key']=$this->session_key;
		return json_decode($this->api($data,$this->phoneUrl),true);
		
		
	}
	
	/**
	 * 
	 * ����״̬
	 * @param String $status ״̬����
	 * @return mixed
	 */
	public function setStatus($status){
		$data=$this->data;
		$data['v']='1.1';
		$data['method']='users.setStatus';
		$data['type']='1';
		$data['status']=$status;
		$data['session_key']=$this->session_key;
		return json_decode($this->api($data,$this->phoneUrl),true);
//		return ($data);
	}
	
	/**
	 * 
	 * �ظ�״̬
	 * @param $uid �û�id���ظ�����
	 * @param $r_id ���ظ����û�id
	 * @param $content �ظ�����
	 * @param $status_id ״̬id
	 */
	public function replyStatus($uid,$r_id,$content,$status_id){
		$data=$this->data;
		$data['v']='1.1';
		$data['method']='users.replyStatus';
		$data['uid']=$uid;
		$data['content']=$content;
		$data['session_key']=$this->session_key;
		$data['status_id']=$status_id;
		$data['r_id']=$r_id;
		return json_decode($this->api($data,$this->phoneUrl),true);
	}
	private function resultSort($result=array()){
		foreach ($result as $key =>$value)
			if(strcmp($value['time'], date("m-d ")."99:99")<0)
				$result[$key]['time']=date("Y-").$value['time'];
			else
				$result[$key]['time']=(date("Y-")-1).$value['time'];
		usort($result, array("renren","feedsCmpByTime"));
		return $result;
	}
//	public function getLatest($count = 10, $page = 0){
//		$offset=$count*$page;
//		$result=$this->getFeedList($count,'10',$offset);
//		array_filter($result,"feedFilter");
//		return $result;
//	
//	}
//	private function feedFilter($var){
//		$ar=array(	"uid"=>$var['from']['0']['id'],
//					"user"=>$var['from']['0']['name'],
//					"id"=>$var['feed_id'],
//					"content"=>$var['content'],
//					"source"=>"renren",
//					"avatar"=>"http://img.m.renren.com/gn?op=resize&w=50&p=".$var['from']['0']['id'],
//					"datetime"=>strtotime($var['time']),
//					);
//					return true;
//	}


	public static function register($username=NULL,$password=NULL){
		if ($username!=null&&$password!=NULL){
			$result=$this->login($username, $password);
		}
		if (isset($result['error']))
			return "failed";
		else{
			
		}
			
	}
	public function getLatest($count=0,$page=0){
		$result=$this->getFeedList($count,$page*$count)	;
		$r=array_map(array("renren","feedReform"), $result);
		return $r;
	}
	public function feedReform($a){
		return array(	"uid"=>$a['from']['0']['id'],
						"id"=>$a['feed_id'],
						"source"=>"RENREN",
						"user"=>$a['from']['0']['name'],
						"avatar"=>"http://img.m.renren.com/gn?op=resize&w=200&p=".$a['from']['0']['id'],		
						"content"=>$a['content'],
						"datetime"=>$a['time'],
						);
	}
	public function update($content){
		$result= $this->setStatus($content);
		
	}
	public function reply($target, $id, $content, $reply_to = null){
	$this->replyStatus($this->uid, $reply_to, $content, $id);	
	}
	public function getProfile(){
		
		$result= $this->_getProfile($this->uid,"basic,feed");
		return array(
					"profile"=>array("username"=>$result['username'],"avatar"=>$result['head_url']),
					"timeline"=>array_map(array("renren","feedReform"), $result['minifeed']));
	}
}
?>